
<?php
/**
 * DCRM Debian List
 *
 * This file is part of WEIPDCRM.
 * 
 * WEIPDCRM is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * WEIPDCRM is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with WEIPDCRM.  If not, see <http://www.gnu.org/licenses/>.
 */

session_start();
define("DCRM",true);
$activeid = 'Code';

if (isset($_SESSION['connected']) && $_SESSION['connected'] === true) {
	require_once("header.php");
	class_loader('CorePage');

	if (!isset($_GET['action'])) {
?>

				<h2><?php _e('激活码管理'); ?></h2>
				<br />
				<h3 class="navbar"><span><?php _e('生成激活码'); ?></span></h3>
                 <form class="form-horizontal" method="GET" action="Code.php" >
					<div class="group-control">
						<label class="control-label">激活码前缀</label>
						<div class="controls">
	
							<input class="input-xlarge" name="codeqz" type="text" />
						</div>
					</div><br>
                   <div class="group-control">
						<label class="control-label">生成数量</label>
						<div class="controls">
	
							<input class="input-xlarge" name="num" type="text" />
						</div>
					</div><br>
						<label class="control-label">激活等级</label>
						<div class="controls">
                          		<select name="prefix" >
                          <?php
							$sections = DB::fetch_all("SELECT * FROM `".DCRM_CON_PREFIX."usercode`");
							foreach($sections as $section){
							?> 
							<option value="<?php echo($section['CodeLevel']); ?>">VIP<?php echo($section['CodeLevel']-1); ?>|<?php echo($section['Name']); ?></option>
							<?php
							}
						?>
							</select>
							<button type="submit" class="btn btn-success">生成激活码</button>
						</div>
					</form>
<button onclick="cookipost1()" type="submit"class="btn btn-success">刷新激活码页面</button>
<?php 
  $date=date("Y-m-d h:i:s"); 
 
 function randomkeys($length)   
{   
   $codeqz=$_GET['codeqz']; 
   $pattern = 'ABCDEFGHIJKLOMNOPQRSTUVWXYZ1234567890';  
    for($i=0;$i<$length;$i++)   
    {   
        $key .= $pattern{mt_rand(0,35)}; 
    }   
       return $codeqz.$key;  
}   
//echo randomkeys(8); 
  
$date=date("Y-m-d h:i:s");  
$servername = "localhost";
$username = "s7420423";
$password = "kui203061";
$dbname = "s7420423";
$conn = new mysqli($servername, $username, $password, $dbname);
// 检测连接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
} 
/*for ($i = 0; $i < $num; $i++) {
$km=getkm(18);
$sql = "INSERT INTO apt_Activation (ID,KM,UDID,OPENID,Level,Activation,TimeStamp,CreateStamp) VALUES ('','$km','未使用','未使用','$prefix','未使用','$date','')";
}
if(isset($_GET['prefix'])){    
$prefix=$_GET['prefix'];
$num=$_GET['num'];  
if ($conn->query($sql) === TRUE) {
  echo "<script> alert('生成激活码成功');parent.location.href='/manage/Code.php'; </script>";  
  echo "<li class='list-group-item'>$km</li>";
    
} else {
    echo "<script>alert('插件信息上传失败')</script>" . $conn->error;   
}
}*/
  
  
  

if(isset($_GET['prefix'])){    
$prefix=$_GET['prefix'];
$num=$_GET['num'];
echo "<li>成功生成".$num."张卡密</li>";
for ($i = 0; $i < $num; $i++) {
	$km=randomkeys(9);
	$sql = "INSERT INTO apt_Activation (ID,KM,UDID,OPENID,Level,LevelName,LevelLogo,Activation,TimeStamp,CreateStamp) VALUES ('','$km','未使用','未使用','$prefix','','','未使用','$date','')";
	if($conn->query($sql) === TRUE) {
     // echo "<script>location.href='/manage/Code.php'; </script>"; 
    
       echo $km.'<br>';
	}
}
//echo '<li class="list-group-item"><a href="download.php?num='.$num.'" class="btn btn-info btn-xs">导出为文本</a></li></ul>';
 // echo $km;
} 
    } 
?>
  <script language="javascript">
    function cookipost1(){
    	parent.location.href='Code.php'; 
	}
  </script> 
<?php
			if (isset($_GET['page'])) {
				$page = $_GET['page'];
			} elseif (isset($_SESSION['page'])) {
				$page = $_SESSION['page'];
			} else {
				$page = 1;
			}
			if ($page <= 0 OR $page >= 100) {
				$page = 1;
			}
			unset($_SESSION['contents']);
			unset($_SESSION['type']);
			$_SESSION['page'] = $page;
			$row_start = $page * 100 - 100;
            $lists = DB::fetch_all("SELECT `ID`,`KM`,`UDID`,`OPENID`,`Level`,`Activation`,`TimeStamp`,`CreateStamp` FROM `".DCRM_CON_PREFIX."Activation` ORDER BY `ID` DESC LIMIT " . (string)$row_start. ",100");
?>
                                 <table class="table"><thead><tr>
								  <th style="width:40px;"><ul class="ctl">ID</ul></th>
								  <th style="width:220px;"><ul class="ctl"><?php _e('卡密'); ?></ul></th>
                                  <th style="width:250px;"><ul class="ctl"><?php _e('卡密等级'); ?></ul></th>                                   
                                  <th style="width:300px;"><ul class="ctl"><?php _e('激活UDID'); ?></ul></th>
                                  <th style="width:200px;"><ul class="ctl"><?php _e('激活OPENID'); ?></ul></th>
                                  <th style="width:300px;"><ul class="ctl"><?php _e('激活状态'); ?></ul></th>
                                  <th style="width:200px;"><ul class="ctl"><?php _e('生成时间'); ?></ul></th>
                                  <th style="width:200px;"><ul class="ctl"><?php _e('激活时间'); ?></ul></th> 
								</tr></thead><tbody>
<?php
			foreach ($lists as $list) {
?>
								<tr>
                                  
									<td height="20"><ul class="ctl"><?php echo($list['ID']); ?></ul></td>
								   <th><ul class="ctl"><?php echo ($list['KM']); ?></ul></th>
								   <th><ul class="ctl"><?php if($list['Level']=='2'){ echo 'VIP1会员';}if($list['Level']=='3'){ echo 'VIP2会员';}if($list['Level']=='4'){ echo 'VIP3会员';}if($list['Level']=='5'){ echo 'VIP4会员';}if($list['Level']=='6'){ echo 'VIP5会员';}if($list['Level']=='7'){ echo '系统管理员';} ?></ul></th>                                  
									<td><ul class="ctl"><?php echo ($list['UDID']); ?></ul></td>
								   <th><ul class="ctl"><?php echo ($list['OPENID']); ?></ul></th>
									<td><ul class="ctl"><?php echo ($list['Activation']); ?></ul></td>
								   <th><ul class="ctl"><?php echo ($list['TimeStamp']); ?></ul></th>
									<td><ul class="ctl"><?php echo ($list['CreateStamp']); ?></ul></td>      
								</tr>
<?php
			}
?>
								</tbody></table>

<?php
			$packages_count = DB::result_first("SELECT count(*) FROM `".DCRM_CON_PREFIX."Activation");
			$params = array('total_rows' => (int)$packages_count, 'method' => 'html', 'parameter' => 'Code.php?page=%page', 'now_page'  => $page, 'list_rows' => 100);
			$page = new Core_Lib_Page($params);
			echo('<div class="page">' . $page->show(2) . '</div>');
		
?>
<?php
} else {
	$_SESSION['referer'] = $_SERVER['REQUEST_URI'];
	header("Location: login.php");
	exit();
}
?> 

